import API from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:'',
    shoppics:'',
    shopphone:'',
    shopname:'',
    lat:'',
    lng:'',
    autoplay:false,
    interval:0,
    duration:1000,
    imgUrls:['../../img/up1.png'],
    newImgUrls:[],
    tempFilePaths:[], 
    isUpPic:true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow: function () {
    var index = this.data.newImgUrls.findIndex(item=>item==wx.getStorageSync('UpPic'))
    console.log(index)
    if (wx.getStorageSync('UpPic') && index == -1) {
      this.setData({
        imgUrls:[]
      })
      this.uploadFile(wx.getStorageSync('UpPic'))
    }
  },
  tofileUp() {
    wx.navigateTo({
      url: '/pages/cutInside/cutInside?type=1'
    })
  },
  upPic() {
    var _this = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)

        _this.setData({
          tempFilePaths: tempFilePaths,
          imgUrls:[]
        })
        for (var k in tempFilePaths) {
          _this.uploadFile(tempFilePaths[k])
        }
      }
    })
  },
  uploadFile(img) {
    console.log(img)
    var _this = this
    wx.showLoading({ title: '图片上传中...' })
    wx.uploadFile({
      url: 'https://www.szhongtu.net/mcbase/public/index.php/wxroutine/quanzi/upload',
      filePath: img,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success(res) {
        wx.hideLoading()
        var data = JSON.parse(res.data)
        if (data.ret == 0) {
         var pic = _this.data.newImgUrls
         pic.unshift(data.url)
         wx.setStorageSync('UpPic', data.url)
          _this.setData({
            newImgUrls:pic
          })
          _this.setData({
            imgUrls:_this.data.newImgUrls,
            isUpPic:false
          })
          console.log(_this.data.imgUrls)
        }
      }
    })
  },
  toSelectAddress () {
    var that = this
    wx.chooseLocation(
      {
        success:(res)=>{
          console.log(res)
          that.setData({
            address:res.address,
            lat: res.latitude,
            lng:res.longitude,
            shopname: res.name
          })
        }
      }
    )
  },
  // 获取宠物店名称
  getPetStoreName (e) {
    this.setData({
      shopname: e.detail.value
    })
  },
  // 获取电话号码
  getMobile (e) {
    this.setData({
      shopphone: e.detail.value
    })
  },
  save () {
    console.log(this.data.imgUrls)
    if (this.data.address == '') {
      wx.showModal({
        content: '请选择地址',
      })
      return
    }
    if (this.data.shopname == '') {
      wx.showModal({
        content: '请输入店铺名称',
      })
      return
    }
    var regex = /^1\d{10}$/
    if (this.data.shopname!='') {
      if (!regex.test(this.data.shopphone)) {
        wx.showModal({
          content: '请输入正确的手机号',
        })
        return
      }
    }

    var data = {
      shopname: this.data.shopname,
      shopphone: this.data.shopphone,
      longitude: this.data.lng,
      latitude: this.data.lat,
      address: this.data.address,
      shoppics: this.data.imgUrls.join(',')
    }
    API.upDoctorData(data).then(res=>{
        if (res.ret == 0) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          
        }
    })
  }

})